Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🎩 What is the goal?
To solve a simple exercise using Validated data type. The exercise statement can be found here. In this exercise, the main goal is to validate a form returning all the errors if there are some.
How is it being implemented?
To implement this Kata we decided to use Refined types in Scala. However, the usage of these types guarantees that the
Form
type should be instantiated in a valid way using literals or should be instantiated usingrefinedV
where the possible validation error should be handled before instantiating any field. That's why we created the typeUnsafeForm
. As most of the time our values are generated in runtime we needed a type to hold all this information and then aFormValidator
we implemented easily thanks to these functions:mapN
. Letting us transform aTuple6
into aValidatedNel<FormError, Form>
. You can see here how the usage of implicits let us don't indicate theNonEmptyList
semigroup instance or the applicative instance we needed in other solutions like the Arrow one written in KotlinWe created our own type named
ValidEmail
and used the already implemented ones to compose the rest of the types. The usage of regex was really interesting combined with the type definition.Validated.fromEither
. Letting us transform theEither
generated byrefineV
into aValidated
as follows:Last but not least, there is an interesting point when taking a look at the test coverage. We couldn't get the generators working for the types based on
MatchesRegex
type. I guess this is because it's not possible to generate values matching the expression in a reasonable time. So in this scenario, we couldn't automatically derive our generators as we did in the previous kata 😢If you are wondering why we used
Validated
instead ofEither
this blog post will resolve your question: https://www.tobyhobson.com/cats-validated/How can it be tested?
Automatically! 🤖 I've added some automated tests I'd recommend you to review 😃